package com.xqiapp.jsp.mybatis;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.xqiapp.bean.Datax;
import com.xqiapp.bean.Page;
import com.xqiapp.bean.PageContainer;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 *
 * 通用数据访问接口实现
 */
//@Component
public class BaseDao implements IMybatisDao {

//	@Autowired
	private SqlSessionTemplate sqlSession;

	@Override
	public int insert(String statement, Object parameter) {
		return sqlSession.insert(statement, parameter);
	}

	@Override
	public int update(String statement, Object parameter) {
		return sqlSession.update(statement, parameter);
	}

	@Override
	public <K, V, T> T getObject(String statement, Map<K, V> parameter) {
		return sqlSession.selectOne(statement, parameter);
	}

	@Override
	public <K, V> Map<K, V> get(String statement, Map<K, V> parameter) {
		return sqlSession.selectOne(statement, parameter);
	}

	@Override
	public <K, V> int delete(String statement, Map<K, V> parameter) {
		return sqlSession.delete(statement, parameter);
	}

	@Override
	public <E, K, V> List<E> find(String statement, Map<K, V> parameter) {
		return sqlSession.selectList(statement, parameter);
	}

	@Override
	public <E> List<E> find(String statement) {
		return sqlSession.selectList(statement);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <E, K, V> Page<E> page(String pageStatement, Map<K, V> parameter, int current, int pagesize) {
		PageBounds pageBounds=null;
		if(parameter.get("sort")==null){
			pageBounds = new PageBounds(current, pagesize);
		}else{
			pageBounds = new PageBounds(current, pagesize, Order.formString(parameter.get("sort").toString()+"."+parameter.get("order").toString()));
		}
		PageList<E> result = (PageList<E>) sqlSession.selectList(pageStatement, parameter, pageBounds);
		Paginator paginator = result.getPaginator();
		return new PageContainer<E, K, V>(paginator.getTotalCount(), paginator.getTotalPages(), result);
	}

	@Override
	public Connection getConnection() {
		return sqlSession.getConnection();
	}

	@Override
	public Configuration getConfiguration() {
		return sqlSession.getConfiguration();
	}

	@Override
	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSession;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public <K, V> int delete(String statement, List ids){
		return sqlSession.delete(statement, ids);
	}


	@Override
	public <E> E getObject(String statement, Datax parameter) {
		return sqlSession.selectOne(statement,parameter);
	}

	@Override
	public <E> E getObject(String statement, E parameter) {
		return sqlSession.selectOne(statement,parameter);
	}

	@Override
	public Datax get(String statement, Datax parameter) {
		return sqlSession.selectOne(statement,parameter);
	}

	@Override
	public int delete(String statement, Datax parameter) {
		return sqlSession.delete(statement,parameter);
	}

	@Override
	public <E> List<E> find(String statement, Datax parameter) {
		return sqlSession.selectList(statement,parameter);
	}

	@Override
	public <E> List<E> find(String statement, Object parameter) {
		return sqlSession.selectList(statement,parameter);
	}

	@Override
	public <E> Page<E> page(String pageStatement, Datax parameter, int current, int pagesize) {
		PageBounds pageBounds=null;
		if(parameter.get("sort")==null){
			pageBounds = new PageBounds(current, pagesize);
		}else{
			pageBounds = new PageBounds(current, pagesize, Order.formString(parameter.get("sort").toString()+"."+parameter.get("order").toString()));
		}
		PageList<E> result = (PageList<E>) sqlSession.selectList(pageStatement, parameter, pageBounds);
		Paginator paginator = result.getPaginator();
		return new PageContainer(paginator.getTotalCount(), paginator.getTotalPages(), result);
	}
}
